package com.ssm.service;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ssm.dao.IUserDao;
import com.ssm.entity.User;

@Service
public class UserServiceImpl implements IUserService {
	@Autowired
	private IUserDao userDao;
	@Override
	public String findAll() {		
		List<User> list = userDao.findAll();
		JSONObject obj = new JSONObject();
		obj.put("draw", 1);
		obj.put("recordsTotal", userDao.count());
		obj.put("recordsFiltered", userDao.count());
		JSONArray data = new JSONArray();
		for(User item:list) {
			JSONArray arr = new JSONArray();
			arr.add(item.getUid());
			arr.add(item.getUname());
			arr.add(item.getAlias());
			arr.add(item.getLast_login_time());
			arr.add(item.getRole().getRname());
			arr.add(item.getStatus());
			data.add(arr);
		}
		obj.put("data", data);
		return obj.toJSONString();
	}
	@Override
	public String add(User user) {
		// 确保密码不为空，如果为空则设置默认密码123456
		if (user.getPasswd() == null || user.getPasswd().trim().isEmpty()) {
			user.setPasswd("123456");
		}
		
		// 检查用户名是否已存在
		User existingUser = userDao.findByUsername(user.getUname());
		if (existingUser != null) {
			JSONObject obj = new JSONObject();
			obj.put("Status", "error");
			obj.put("msg", "用户名已存在");
			return obj.toJSONString();
		}
		
		// 执行添加操作
		int count = userDao.add(user);
		String status = "ok";
		String msg = "添加成功";
		if(count<1) {
			status = "error";
			msg = "添加失败";
		}
		JSONObject obj=new JSONObject();
		obj.put("Status", status);
		obj.put("msg", msg);
		return obj.toJSONString();
	}
	@Override
	public String get(int id) {
		User user = userDao.get(id);
		JSONObject obj=new JSONObject();		 
		obj.put("uid", user.getUid());
		obj.put("uname", user.getUname());
		obj.put("alias", user.getAlias());
		obj.put("passwd", user.getPasswd());
		obj.put("logo", user.getLogo());		
		obj.put("last_login_time", user.getLast_login_time());
		obj.put("status", user.getStatus());
		obj.put("rid", user.getRole().getRid());
		return obj.toJSONString();
	}
	@Override
	public String update(User user) {
		int count = userDao.update(user);
		String status = "ok";
		String msg = "";
		if(count<1) {
			status = "error";	
			msg = "更新失败";
		}
		JSONObject obj=new JSONObject();
		obj.put("Status", status);
		obj.put("msg", msg);
		return obj.toJSONString();
	}
	@Override
	public String delete(int id) {
		int count = userDao.delete(id);
		String status = "ok";
		String msg = "";
		if(count<1) {
			status = "error";	
			msg = "删除失败";
		}
		JSONObject obj=new JSONObject();
		obj.put("Status", status);
		obj.put("msg", msg);
		return obj.toJSONString();
	}
	@Override
	public Boolean login(User user) {
		String passwdStr = userDao.getPasswdByUserName(user);
		// 确保密码不为null，并进行trim后比较
		if (passwdStr != null && user.getPasswd() != null) {
			return passwdStr.trim().equals(user.getPasswd().trim());
		}
		return false;
    }
    
    @Override
    public String getUserIdByName(String username) {
        return userDao.getUserIdByName(username);
    }
    
    @Override
    public User findByUsername(String username) {
        return userDao.findByUsername(username);
    }
    
    @Override
    public String findRoleNameById(int roleId) {
        return userDao.findRoleNameById(roleId);
    }
    
    @Override
	public User save(User user) {
        // 确保密码不为空，如果为空则设置默认密码123456
        if (user.getPasswd() == null || user.getPasswd().trim().isEmpty()) {
            user.setPasswd("123456");
        }
        
        // 检查用户名是否已存在
        User existingUser = userDao.findByUsername(user.getUname());
        if (existingUser != null) {
            return null; // 用户名已存在
        }
        
        userDao.add(user);
        return user;
    }
    
    public void updateLastLoginTime(int userId) {
        userDao.updateLastLoginTime(userId);
    }
}
